package com.noah.project.controller

import com.noah.project.global.Const
import com.noah.project.global.JsonManager
import com.noah.project.dao.CommentDao
import com.noah.project.dao.NoteDao
import com.noah.project.global.TokenManager
import com.noah.project.model.Comment
import com.noah.project.model.Reply
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.scheduling.annotation.EnableAsync
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


@EnableAsync
@RestController
class CommentApiController {
    @Autowired
    lateinit var noteDao: NoteDao
    @Autowired
    lateinit var commentDao: CommentDao
    val log = LoggerFactory.getLogger(CommentApiController::class.simpleName)

    /*****************评论******************/
    @RequestMapping(Const.API_ADD_COMMENT)
    fun addComment(comment: Comment, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            if (comment.nid != null) {
                val retComment = noteDao.findById(comment.nid)
                if (retComment == null) {
                    return Reply(code = Const.FAIL_NONE, msg = "日记不存在")
                }
                comment.cTime = System.currentTimeMillis()
                val retCid = commentDao.insert(comment)
                if (retCid > 0) {
                    return Reply(code = Const.SUCCESS, msg = "发布评论成功", data = JsonManager.toJson(comment))
                }
            }
            return Reply(code = Const.FAIL, msg = "发布评论失败")
        } catch (e: Exception) {
            log.error("创建评论", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "评论异常")
        }
    }

    @RequestMapping(Const.API_DELETE_COMMENT)
    fun deleteComment(cid: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val ret = commentDao.delete(cid)
            if (ret > 0) {
                return Reply(code = Const.SUCCESS, msg = "删除评论成功")
            }
            return Reply(code = Const.FAIL, msg = "删除评论失败")
        } catch (e: Exception) {
            log.error("删除评论", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "评论异常")
        }
    }

    @RequestMapping(Const.API_UPDATE_COMMENT)
    fun updateComment(comment: Comment, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val ret = commentDao.update(comment)
            if (ret > 0) {
                val retNote = commentDao.findById(comment.cid)
                if (retNote != null) {
                    return Reply(code = Const.SUCCESS, msg = "修改评论成功", data = JsonManager.toJson(retNote))
                }
            }
            return Reply(code = Const.FAIL, msg = "修改评论失败")
        } catch (e: Exception) {
            log.error("更新评论", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "评论异常")
        }
    }

    @RequestMapping(Const.API_GET_COMMENT_LIST)
    fun getCommentList(nid: Long, time: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val retNotes = commentDao.findCommentList(nid, time)
            if (retNotes.isNotEmpty()) {
                return Reply(code = Const.SUCCESS, msg = "获取评论列表成功", data = JsonManager.toJson(retNotes))
            }
            return Reply(code = Const.FAIL, msg = "找不到该日记的评论")
        } catch (e: Exception) {
            log.error("获取评论列表", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "评论异常")
        }
    }

    @RequestMapping(Const.API_GET_COMMENT)
    fun getComment(cid: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val retNote = commentDao.findComment(cid)
            if (retNote != null) {
                return Reply(code = Const.SUCCESS, msg = "获取评论成功", data = JsonManager.toJson(retNote))
            }
            return Reply(code = Const.FAIL, msg = "找不到该日记的评论")
        } catch (e: Exception) {
            log.error("获取单个评论", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "评论异常")
        }
    }


    fun connect() {
        commentDao.findCommentList(1, 0)
    }


    fun testThread(): Reply {
        //log.info("=========》当前线程名：" + Thread.currentThread().name)
        var retNotes = commentDao.findById(3)
        return Reply(code = Const.SUCCESS, msg = "获取评论成功", data = JsonManager.toJson(retNotes))
    }
}